﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Utility;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace App.Account.Models.Forms
{
    public class UserForm : IRuleEntity
    {
        public Guid? UserID { get; set; }
        public string UserName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public List<Guid> Roles { get; set; }

        public List<RuleViolation> GetRuleViolations()
        {
            List<RuleViolation> validationIssues = new List<RuleViolation>();


            // Basic parameter validation
            if (String.IsNullOrEmpty(UserName))
            {
                validationIssues.Add(new RuleViolation()
                {
                    PropertyName = "UserName",
                    ErrorMessage = "You must specify a username."
                });
            }
            if (String.IsNullOrEmpty(Email))
            {
                validationIssues.Add(new RuleViolation()
                {
                    PropertyName = "Email",
                    ErrorMessage = "You must specify an email address."
                });
            }
            else
            {
                if (!Regex.IsMatch(Email, @"^(?i:(?<local_part>[a-z0-9!#$%^&*{}'`+=-_|/?]+(?:\.[a-z0-9!#$%^&*{}'`+=-_|/?]+)*)@(?<labels>[a-z0-9]+\z?.*[a-z0-9-_]+)*(?<tld>\.[a-z0-9]{2,}))$"))
                    validationIssues.Add(new RuleViolation()
                    {
                        PropertyName = "Email",
                        ErrorMessage = "You must specify a valid email address."
                    });
            }

            if (Password == null || Password.Length < Properties.Settings.Default.MinRequiredPasswordLength)
            {
                validationIssues.Add(new RuleViolation()
                {
                    PropertyName = "Password",
                    ErrorMessage =
                    String.Format(CultureInfo.CurrentCulture,
                         "You must specify a password of {0} or more characters.",
                         Properties.Settings.Default.MinRequiredPasswordLength)
                });
            }

            return validationIssues;
        }
    }
}
